﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Threading;

namespace HuiZe.education.View.Ethan
{
    /// <summary>
    /// PART_Media_MP3.xaml 的交互逻辑
    /// </summary>
    public partial class PART_Media_MP3 : UserControl
    {
        public bool IsAutoPlay { get; set; }

        public PART_Media_MP3()
        {
            InitializeComponent();
            _is_opended = false;
            PART_Play.Click += PART_Play_Click;
            PART_Volume.Click += PART_Volume_Click;
            PART_Player.MediaOpened += PART_Player_MediaOpened;
            PART_Slider_Volume.ValueChanged += PART_Slider_Volume_ValueChanged;
            PART_Slider_Position.ValueChanged += PART_Slider_Position_ValueChanged;
            Mp3PlayingStotyboard = (Storyboard)FindResource("Mp3PlayingStotyboard");
        }

        public void SetMP3(string _path)
        {
            _is_mute = false;
            _is_pause = true;
            _volume_last = 0.0;
            _position_all = string.Empty;
            PART_Volume.Content = "静音";
            PART_Player.Source = new Uri(_path, UriKind.Absolute);
            _is_opended = string.IsNullOrEmpty(_path_lastest) || _path_lastest != _path;
            if (PART_Lyric.Visibility != Visibility.Visible && PART_Images.Visibility != Visibility.Visible)
                PART_RunningImage.Visibility = Visibility.Visible;

            if (!_is_opended)
                PART_Player.Play();

            _path_lastest = _path;
        }

        public void SetLyric(string _lyric_text)
        {
            _lyric_seconds = null;
            PART_Lyric.Visibility = Visibility.Collapsed;
            PART_Lyric_Mask.Visibility = Visibility.Collapsed;
            if (!string.IsNullOrEmpty(_lyric_text))
            {
                PART_Lyric.SetLyric(_lyric_text);
                _lyric_seconds = PART_Lyric.LyricMillSeconds;
                if (_lyric_seconds != null && _lyric_seconds.Length > 0)
                {
                    PART_Lyric.Visibility = Visibility.Visible;
                    PART_Lyric_Mask.Visibility = Visibility.Visible;
                }
            }
        }

        public void SetImageList(string[] _list_image, int _seconds)
        {
            _switch_seconds = _seconds;
            PART_Images.Visibility = Visibility.Collapsed;

            if (_list_image != null && _list_image.Length > 0)
            {
                PART_Images.Visibility = Visibility.Visible;
                PART_Images.SetImageList(_list_image);
            }
        }

        public void PlayMp3()
        {
            PART_Play_Click(this, new RoutedEventArgs());
        }

        void PART_Volume_Click(object sender, RoutedEventArgs e)
        {
            if (_is_mute)
            {
                _is_mute = false;
                PART_Volume.Content = "静音";
                PART_Slider_Volume.IsEnabled = true;
                PART_Slider_Volume.Value = _volume_last;
            }
            else
            {
                _is_mute = true;
                PART_Volume.Content = "音量";
                PART_Slider_Volume.IsEnabled = false;
                _volume_last = PART_Slider_Volume.Value;
                PART_Slider_Volume.Value = 0.0;
            }
        }

        void PART_Play_Click(object sender, RoutedEventArgs e)
        {
            if (_is_pause)
            {
                _is_pause = false;
                Play();
            }
            else
            {
                _is_pause = true;
                Pause();
            }
        }

        void PART_Slider_Volume_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            PART_Player.Volume = e.NewValue;
        }

        void PART_Slider_Position_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            PART_Player.Position = TimeSpan.FromSeconds(e.NewValue);
        }

        void PART_Player_MediaOpened(object sender, RoutedEventArgs e)
        {
            if (PART_Slider_Volume.Value == 0.0) PART_Slider_Volume.Value = 0.5;
            PART_Slider_Position.Maximum = PART_Player.NaturalDuration.TimeSpan.TotalSeconds;
            _position_all = $"{PART_Player.NaturalDuration.TimeSpan.Minutes.ToString("D2")}:{PART_Player.NaturalDuration.TimeSpan.Seconds.ToString("D2")}";

            PART_Position.Text = $"00:00 / {_position_all}";
            PART_Player.Stop();

            if (IsAutoPlay)
                PlayMp3();

            _is_opended = true;
        }

        void _timer_Tick(object sender, EventArgs e)
        {
            int _seconds = PART_Player.Position.Minutes * 60 + PART_Player.Position.Seconds;
            if (_lyric_seconds != null && _lyric_seconds.Length > 0 && _lyric_seconds.Contains(_seconds))
            {
                PART_Lyric.MoveNext(_seconds);
            }
            PART_Slider_Position.Value = PART_Player.Position.TotalSeconds;
            PART_Position.Text = $"{PART_Player.Position.Minutes.ToString("D2")}:{PART_Player.Position.Seconds.ToString("D2")} / {_position_all}";
            if (PART_Images.Visibility == Visibility.Visible && _switch_seconds > 0 && _seconds % _switch_seconds == 0)
            {
                PART_Images.MoveNext();
            }
        }

        void Play()
        {
            if (PART_Lyric.Visibility != Visibility.Visible && PART_Images.Visibility != Visibility.Visible)
            {
                PART_RunningImage.Visibility = Visibility.Visible;
                if (!_is_started)
                {
                    _is_started = true;
                    Mp3PlayingStotyboard.Begin();
                }
                else
                {
                    Mp3PlayingStotyboard.Resume();
                }
            }

            PART_Play.Content = "暂停";
            PART_Player.Play();
            if (_timer == null)
            {
                _timer = new DispatcherTimer();
                _timer.Interval = TimeSpan.FromSeconds(1);
                _timer.Tick += _timer_Tick;
            }
            if (_timer != null)
                _timer.Start();
        }

        void Pause()
        {
            PART_Play.Content = "播放";
            PART_Player.Pause();
            if (_timer != null)
                _timer.Stop();
            if (PART_Lyric.Visibility != Visibility.Visible && PART_Images.Visibility != Visibility.Visible)
            {
                Mp3PlayingStotyboard.Pause();
            }
        }

        public void Reset()
        {
            PART_Player.Stop();
            PART_Play.Content = "播放";
            PART_Slider_Position.Value = 0.0;
            PART_RunningImage.Visibility = Visibility.Collapsed;
            if (_timer != null)
            {
                _timer.Stop();
                _timer.IsEnabled = false;
                _timer = null;
            }
            if (PART_Lyric.Visibility != Visibility.Visible && PART_Images.Visibility != Visibility.Visible && _is_started)
            {
                Mp3PlayingStotyboard.Pause();
            }
        }


        bool _is_mute;
        bool _is_pause;
        bool _is_opended;
        bool _is_started;
        string _path_lastest;
        string _position_all;
        int _switch_seconds;
        DispatcherTimer _timer;
        double _volume_last = 0.0;
        int[] _lyric_seconds;
        Storyboard Mp3PlayingStotyboard;
    }
}
